/*
Assume a system where the value returned by the call sysconf(_SC_CLK_TCK) is
100. Assuming that the clock_t value returned by times() is an unsigned 32-bit
integer, how long will it take before this value cycles so that it restarts at 0? Perform
the same calculation for the CLOCKS_PER_SEC value returned by clock()
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/times.h>

long
number_clock_ticks_per_second(void)
{
  printf("CLOCKS_PER_SEC=%ld sysconf(_SC_CLK_TCK)=%ld\n\n",
         (long)CLOCKS_PER_SEC, sysconf(_SC_CLK_TCK));
}

void
display_process_time(void)
{
  struct tms timestamp;
  clock_t clock = times(&timestamp);
  if (clock == -1)
    {
      perror("unable to obtain elapsed time");
      _exit(EXIT_FAILURE);
    }
  printf("user time: %ld\n"
          "system time: %ld\n"
          "user children time: %ld\n"
          "system children time: %ld\n",
         timestamp.tms_utime, timestamp.tms_stime,
         timestamp.tms_cutime, timestamp.tms_cstime);
  printf ("elapsed time: %.2f\n", (double)clock / CLOCKS_PER_SEC);
}

int
main(int argc, char *argv[])
{
  if (argc != 2)
    {
      fprintf(stderr, "put seconds to wait\n");
      _exit(EXIT_FAILURE);
    }
  number_clock_ticks_per_second();
  int count = atoi(argv[1]);
  while (count--)
    {
      display_process_time();
      puts("---------------------------------\n");
      (void) getppid();
      sleep(1);
    }

  return EXIT_SUCCESS;
}
